package com.zmn.mcc.admin.controller.staff.staffcontract;


import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.staff.staffcontract.StaffContract;
import com.zmn.mcc.services.interfaces.staff.staffcontract.StaffContractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Optional;


/**
 * 员工合同信息基础修改Controller
 *
 * @author tanghongjun
 * @since 2020-05-14 11:15:18
 */
@RestController
@RequestMapping("/staff/staffcontract")
public class StaffContractModifyController extends StaffContractBaseController {
    private Logger logger = LoggerFactory.getLogger(StaffContractListController.class);

    @Resource
    private StaffContractService staffContractService;

    /**
     * 保存
     */
    @RequestMapping("/addEdit")
//    @RequiresPermissions("staff/staffcontract/addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody StaffContract staffContract) {

        Date now = DateUtil.getNow();

        int contractId = Optional.ofNullable(staffContract.getContractId()).orElse(0);

        try {
            if (contractId == 0) {
                staffContract.setCreater(getStaffName());
                staffContract.setCreateTime(now);
                staffContractService.insert(staffContract);
            } else {
                staffContract.setUpdater(getStaffName());
                staffContract.setUpdateTime(now);
                staffContractService.updateStaffContractByKey(staffContract);
            }

            return ResponseDTO.success("操作成功！");
        } catch (Exception e) {
            logger.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 删除
     *
     * @param contractId
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
//    @RequiresPermissions("staff/staffcontract/delete")
    public ResponseDTO delete(@RequestParam(value = "contractId", defaultValue = "0") Integer contractId) {
        if (NumberUtil.isNullOrZero(contractId)) {
            return ResponseDTO.fail("请选择要删除的数据！");
        }
        staffContractService.deleteByKey(contractId);
        return ResponseDTO.success("删除成功！");
    }

}
